package com.yhx.controller;


import com.yhx.pojo.User;
import com.yhx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 使用RestTemplate调用服务
 */
@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserService userService;

    //用Ribbon做负载均衡时，这里的服务地址应该是一个变量，通过服务名来访问
    //private static final String REST_URL_PREFIX = "http://localhost:8081";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER";


    @RequestMapping("/consumer/user/queryUser/{id}")
    public User queryUser(@PathVariable("id") Integer id){
        User user = restTemplate.getForObject(REST_URL_PREFIX + "/user/queryUser/"+id, User.class);
        return user;
    }

    @RequestMapping("/consumer/user/queryUsers")
    public List<User> queryUsers(){
        List<User> users = restTemplate.getForObject(REST_URL_PREFIX + "/user/queryUsers", List.class);
        return users;
    }

    @RequestMapping("/consumer/user/queryUserByName/{name}")
    public List<User> queryUserByName(@PathVariable("name") String name){
        List<User> users = userService.queryUserByName(name);
        return users;
    }
}
